package com.heima.comment.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.heima.client.article.IArticleClient;
import com.heima.client.behavior.IBehaviorClient;
import com.heima.client.wemedia.IWemediaClient;
import com.heima.comment.service.CommentService;
import com.heima.common.constants.CommentConstants;
import com.heima.common.exception.CustomException;
import com.heima.common.redis.CacheService;
import com.heima.model.common.dtos.*;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.common.pojos.article.ApAuthor;
import com.heima.model.common.pojos.comment.ApComment;
import com.heima.model.common.pojos.comment.ApCommentLike;
import com.heima.model.common.pojos.comment.ApCommentRepay;
import com.heima.model.common.pojos.comment.ApCommentRepayLike;
import com.heima.model.common.pojos.wemedia.WmSensitive;
import com.heima.model.common.vo.ApCommentRepayVo;
import com.heima.model.common.vo.ApCommentVo;
import com.heima.utils.common.SensitiveWordUtil;
import com.heima.utils.thread.ApThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
public class CommentServiceImpl implements CommentService {

    @Autowired
    private IWemediaClient iWemediaClient;

    @Autowired
    private IArticleClient iArticleClient;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private CacheService cacheService;

    @Autowired
    private IBehaviorClient iLikeBehavior;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * app端文章发布评论
     *
     * @param commentSaveDto
     * @return
     */
    @Override
    public ResponseResult save(CommentSaveDto commentSaveDto) {
        //1、参数校验
        if (commentSaveDto == null || commentSaveDto.getArticleId() == null
                || StringUtils.isEmpty(commentSaveDto.getContent())) {
            //抛出参数异常
            throw new CustomException(AppHttpCodeEnum.PARAM_INVALID);
        }

        //1.2验证当前用户是否登录
        Integer userId = ApThreadLocalUtil.get();

        //1.3如果为空提示需要登录
        if (userId == null) {
            //抛出需要登录异常
            throw new CustomException(AppHttpCodeEnum.NEED_LOGIN);
        }

        //1.4、判断评论内容是否超过140个字
        if (commentSaveDto.getContent().length() >= CommentConstants.COMMENTS_ONE_HUNDRED_AND_FORTY) {
            //抛出评论字数超出限制异常
            throw new CustomException(AppHttpCodeEnum.COMMENTS_ONE_HUNDRED_AND_FORTY_ERROR);
        }


        //2、文本审核 先内审，走自定义敏感词库过滤、再外审
        //2.1 加载系统敏感词数据
        //缓存优化：先查缓存，缓存有：直接用，缓存没有：再查数据库，并将查到的数据添加到缓存
        String sensitivesJSON = cacheService.get("comment_sensitives");
        List<String> sensitiveList = JSONArray.parseArray(sensitivesJSON, String.class);
        //缓存没有：再查数据库远程调用 wm_sensitive表中的敏感词数据
        if (sensitiveList == null) {
            List<WmSensitive> wmSensitives = iWemediaClient.autoScan();
            sensitiveList = wmSensitives.stream().map(WmSensitive::getSensitives).collect(Collectors.toList());
            //添加到缓存
            cacheService.set("comment_sensitives", JSON.toJSONString(sensitiveList));
        }
        //2.2 初始化DFA算法字典
        SensitiveWordUtil.initMap(sensitiveList);
        //2.3 调用DFA算法进行指定文本的敏感词过滤
        Map<String, Integer> resMap = SensitiveWordUtil.matchWords(commentSaveDto.getContent());

        //2.4判断结果不为空说明匹配到了 有敏感词
        if (resMap != null && resMap.size() > 0) {
            //有敏感词不合规，直接结束，就不需要外审
            return ResponseResult.errorResult(AppHttpCodeEnum.COMMENTS_IS_NON_COMPLIANCE_ERROR);
        }

        //3. 外审：调用百度审核接口审核评论内容  审核结果类型，可取值1、2、3、4，分别代表1：合规，2：不合规，3：疑似，4：审核失败
        ResponseResult responseResult = iWemediaClient.scanText(commentSaveDto.getContent());


        log.info("responseResult返回的数据为：{}", responseResult);

        //3.1判断状态码 200为成功
        if (responseResult.getCode() != 200) {
            //状态不为200 直接返回错误
            return ResponseResult.errorResult(AppHttpCodeEnum.COMMENTS_IS_NON_COMPLIANCE_ERROR);
        }

        //3.2、判断返回的结果
        if (responseResult.getData() == CommentConstants.TWO || responseResult.getData() == CommentConstants.THREE
                || responseResult.getData() == CommentConstants.FOUR) {
            //返回错误结果
            return ResponseResult.errorResult(AppHttpCodeEnum.COMMENTS_IS_NON_COMPLIANCE_ERROR);
        }

        //4、结果等于1 则审核通过、评论发布成功 存储到MongoDB中
        //4.1通过工具类将dto数据复制到实体类中
        ApComment apComment = new ApComment();
        BeanUtils.copyProperties(commentSaveDto, apComment);
        //4.2封装数据  作者id也就是用户id author entryId文章id
        apComment.setAuthorId(userId);
        apComment.setEntryId(commentSaveDto.getArticleId());
        apComment.setType(2);           // 默认值 评论
        //4.3apComment.setLikes(null);  // 先不设置 todo：后面可能会修改
        apComment.setReply(0);          // 默认第一个是未回复
        apComment.setFlag(0);           // 默认值0 评论
        apComment.setCreatedTime(new Date());

        //4.4远程调用拿到作者名称
        ResponseResult response = iArticleClient.queryByUserId(userId);

        //4.5 获取里面数据
        Object data = response.getData();

        //4.6 将数据转换成ApAuthor对象
        ApAuthor apAuthor = JSON.parseObject(JSON.toJSONString(data), ApAuthor.class);

        log.info("获取到apAuthor为: {}", apAuthor);

        //4.6 设置作者名称
        apComment.setAuthorName(apAuthor.getName());

        log.info("apComment的数据为：{}", apComment);

        //4.4保存至 mongoDB数据库 ap_comment表
        mongoTemplate.save(apComment);
        //todo: 后面维护一下文章表中的评论数量
        //远程调用将文章表中的评论数量加一
       // iArticleClient.updateCommentCount(apAuthor);
        //5、返回结果
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);

    }

    /**
     * app端文章评论列表
     *
     * @param commentDto
     * @return
     */
    @Override
    public ResponseResult loadCommentList(CommentDto commentDto) {
        int page = 0;
        int size = 10;
        //1、参数校验
        if (commentDto == null || commentDto.getArticleId() == null || commentDto.getMinDate() == null) {
            //抛出参数异常
            throw new CustomException(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2、获取当前用户id
        Integer userId = ApThreadLocalUtil.get();

        //2.2判断当前用户是否登录
        if (userId == null) {
            //抛出需要登录异常
            throw new CustomException(AppHttpCodeEnum.NEED_LOGIN);
        }

        //3、查询评论列表，根据当前文章进行检索，按照创建时间倒序，分页查询（默认10条数据）
        List<ApComment> apCommentList = mongoTemplate.find(Query.query(Criteria
                                .where("entryId").is(commentDto.getArticleId())     //文章id
                                .and("createdTime").lt(commentDto.getMinDate()))     //查询创建时间小于当前时间//分页
                        .with(Sort.by(Sort.Direction.DESC, "createdTime")).limit(10)
                //根据创建时间倒序
                , ApComment.class);


        //3.2 判断评论列表是否为空
        if (apCommentList == null || apCommentList.size() == 0) {
            //抛出参数异常
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }

        //3.2 在结果返回中，如果当天登录人点赞了某条评论，需要高亮展示“**点赞按钮**”
        List<ApCommentVo> collect = apCommentList.stream().map(apComment -> {
            //创建ApCommentsVo对象
            ApCommentVo apCommentVo = new ApCommentVo();
            //数据拷贝
            BeanUtils.copyProperties(apComment, apCommentVo);
            //根据当前用户id和当前评论的id 查询对应的ApCommentLike数据
            List<ApCommentLike> apCommentLikes = mongoTemplate.find(Query.query(Criteria
                    .where("authorId").is(userId)           //用户id
                    .and("commentId").is(apComment.getId())), ApCommentLike.class); //根据当前用户id和当前评论的id 查询对应的ApCommentLike数据
            //如果apCommentLikes集合不为空，说明当前评论被当前用户点赞
            if (apCommentLikes != null && apCommentLikes.size() > 0) {
                //设置点赞状态
                apCommentVo.setOperation(0);
            }
            //返回
            return apCommentVo;
        }).collect(Collectors.toList());

        //4、返回结果
        return ResponseResult.okResult(collect);
    }

    /**
     * app端文章评论点赞
     *
     * @param commentLikeDto
     * @return
     */
    @Override
    public ResponseResult commentLike(CommentLikeDto commentLikeDto) {
        //参数校验
        if (commentLikeDto == null) {
            //抛出参数异常
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //封装 LikesBehaviorDto  d对象 封装数据
        LikesBehaviorDto likesBehaviorDto = new LikesBehaviorDto();
        BeanUtils.copyProperties(commentLikeDto, likesBehaviorDto);
        //获取当前用户id
        Integer userId = ApThreadLocalUtil.get();
       /* log.info("LikesBehaviorDto的数据为：{}", likesBehaviorDto);
        //远程调用
        ResponseResult like = iLikeBehavior.like(likesBehaviorDto);
        if (like.getCode() != 200) {
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }*/
//        存入redis
        getResult(likesBehaviorDto, userId);
        //todo 不确定数据是否需要redis记录该行为 后期不惜要再删除 redis的调用
        //3将数据存入mongDB 的指定 leadnews-comment.ap_comment_like中
        //ap_comment中的likes +1
        //创建ApCommentLike实体类 存储评论关系

        //获取对应评论的数据
        ApComment apComment = mongoTemplate.findById(commentLikeDto.getCommentId(), ApComment.class);
        if (apComment.getLikes() == null) {
            apComment.setLikes(0);
        }
        //判断用户是否点过赞
        ApCommentLike apCommentLike = mongoTemplate.findOne(Query.query(Criteria.where("commentId").is(commentLikeDto.getCommentId()).and("authorId").is(ApThreadLocalUtil.get())), ApCommentLike.class);
        if (apCommentLike != null && commentLikeDto.getOperation() == 0) {
            //说明用户点过赞
            return ResponseResult.errorResult(AppHttpCodeEnum.ALREADY_LIKE_ERROR);
        }
        //3.1判断是点赞还是取消点赞
        if (likesBehaviorDto.getOperation() == 0) {
            //点赞
            //查询对应评论的数据
            //修改likes数据+1
            apComment.setLikes(apComment.getLikes() + 1);
            //将userId跟评论关系存入到 ap_comment_like
            //拷贝数据
            log.info("apCommentLike的数据为：{}", apCommentLike);
            ApCommentLike apCommentLike1 = new ApCommentLike();
            apCommentLike1.setCommentId(apComment.getId());
            //获取当前用户id
            apCommentLike1.setAuthorId(userId);
            mongoTemplate.save(apCommentLike1);
        } else {
            //取消点赞
            //点赞数量-1
            if (apComment.getLikes() > 0) {
                apComment.setLikes(apComment.getLikes() - 1);
            } else {
                apComment.setLikes(0);
            }
            //删除 根据文章id 和userId 删除对应的 ap_comment_like 点赞关系
            mongoTemplate.remove(apCommentLike);
        }
        //
        Map<String, Integer> likeMap = new HashMap<>();
        likeMap.put("likes", apComment.getLikes());
        return ResponseResult.okResult(likeMap);
    }


    /**
     * app端回复评论点赞
     *
     * @param commentRepayLikeDto
     * @return
     */
    @Override
    public ResponseResult commentRepayLike(CommentRepayLikeDto commentRepayLikeDto) {
        //参数校验
        if (commentRepayLikeDto == null) {
            //抛出参数异常
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //封装 LikesBehaviorDto  d对象
        LikesBehaviorDto likesBehaviorDto = new LikesBehaviorDto();
        BeanUtils.copyProperties(commentRepayLikeDto, likesBehaviorDto);
        //远程调用
//        ResponseResult like = iLikeBehavior.like(likesBehaviorDto);
//        if (like.getCode() != 200) {
//            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
//        }
        //3.1判断是点赞还是取消点赞 存入redis
        Integer userId = ApThreadLocalUtil.get();
        //存入redis
        getResult(likesBehaviorDto, userId);
        //todo 不确定数据是否需要redis记录该行为 后期不惜要再删除 redis的调用
        //3将数据存入mongDB 的指定 leadnews-comment.ap_comment_like中
        //ap_comment_repay中的likes +1
        //获取对应回复评论的数据
        ApCommentRepay apCommentRepay = mongoTemplate.findById(commentRepayLikeDto.getCommentRepayId(), ApCommentRepay.class);
        if (apCommentRepay.getLikes() == null) {
            apCommentRepay.setLikes(0);
        }
        //判断用户是否点过赞
        ApCommentRepayLike apCommentRepayLike = mongoTemplate.findOne(Query.query(Criteria.where("commentRepayId").is(commentRepayLikeDto.getCommentRepayId()).and("authorId").is(ApThreadLocalUtil.get())), ApCommentRepayLike.class);
        if (apCommentRepayLike != null && commentRepayLikeDto.getOperation() == 0) {
            //说明用户点过赞 返回点赞过的信息
            return ResponseResult.errorResult(AppHttpCodeEnum.ALREADY_LIKE_ERROR);
        }
        //判断是点赞还是取消点赞
        if (commentRepayLikeDto.getOperation() == 0) {
            //点赞
            //查询对应评论的数据
            //修改likes数据+1
            apCommentRepay.setLikes(apCommentRepay.getLikes() + 1);
            //将userId跟评论关系存入到 ap_comment_repay_like
            //拷贝数据
            //创先新对象粗出数据
            ApCommentRepayLike apCommentRepayLike1 = new ApCommentRepayLike();
            apCommentRepayLike1.setCommentRepayId(apCommentRepay.getId());
            //获取当前用户id
            apCommentRepayLike1.setAuthorId(ApThreadLocalUtil.get());
            mongoTemplate.save(apCommentRepayLike1);
        } else {
            //取消点赞
            //点赞数量-1
            if (apCommentRepay.getLikes() > 0) {
                apCommentRepay.setLikes(apCommentRepay.getLikes() - 1);

            } else {
                apCommentRepay.setLikes(0);
            }
            //删除 根据文章id 和userId 删除对应的 ap_comment_like 点赞关系
            mongoTemplate.remove(apCommentRepay);
        }
        Map<String, Integer> likeMap = new HashMap<>();
        likeMap.put("likes", apCommentRepay.getLikes());
        return ResponseResult.okResult(likeMap);
    }

    /**
     * app端文章评论回复
     *
     * @param commentRepaySaveDto
     * @return
     */
    @Override
    public ResponseResult saveRepay(CommentRepaySaveDto commentRepaySaveDto) {
        //1、参数校验
        if (commentRepaySaveDto == null || commentRepaySaveDto.getCommentId() == null
                || commentRepaySaveDto.getContent() == null) {
            //抛出参数异常
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //1.2验证当前用户是否登录
        Integer userId = ApThreadLocalUtil.get();

        //1.3如果为空提示需要登录
        if (userId == null) {
            //抛出需要登录异常
            throw new CustomException(AppHttpCodeEnum.NEED_LOGIN);
        }

        //1.4、判断评论内容是否超过140个字
        if (commentRepaySaveDto.getContent().length() >= CommentConstants.COMMENTS_ONE_HUNDRED_AND_FORTY) {
            //抛出评论字数超出限制异常
            throw new CustomException(AppHttpCodeEnum.COMMENTS_ONE_HUNDRED_AND_FORTY_ERROR);
        }

        //2、文本审核 先内审，走自定义敏感词库过滤、再外审
        //2.1 加载系统敏感词数据
        //缓存优化：先查缓存，缓存有：直接用，缓存没有：再查数据库，并将查到的数据添加到缓存
        String sensitivesJSON = cacheService.get("comment_sensitives");
        List<String> sensitiveList = JSONArray.parseArray(sensitivesJSON, String.class);
        //缓存没有：再查数据库远程调用 wm_sensitive表中的敏感词数据
        if (sensitiveList == null) {
            List<WmSensitive> wmSensitives = iWemediaClient.autoScan();
            sensitiveList = wmSensitives.stream().map(WmSensitive::getSensitives).collect(Collectors.toList());
            //添加到缓存
            cacheService.set("comment_sensitives", JSON.toJSONString(sensitiveList));
        }
        //2.2 初始化DFA算法字典
        SensitiveWordUtil.initMap(sensitiveList);
        //2.3 调用DFA算法进行指定文本的敏感词过滤
        Map<String, Integer> resMap = SensitiveWordUtil.matchWords(commentRepaySaveDto.getContent());

        //2.4判断结果不为空说明匹配到了 有敏感词
        if (resMap != null && resMap.size() > 0) {
            //有敏感词不合规，直接结束，就不需要外审
            return ResponseResult.errorResult(AppHttpCodeEnum.COMMENTS_IS_NON_COMPLIANCE_ERROR);
        }

        //3. 外审：调用百度审核接口审核评论内容  审核结果类型，可取值1、2、3、4，分别代表1：合规，2：不合规，3：疑似，4：审核失败
        ResponseResult responseResult = iWemediaClient.scanText(commentRepaySaveDto.getContent());

        log.info("responseResult返回的数据为：{}", responseResult);

        //3.1判断状态码 200为成功
        if (responseResult.getCode() != 200) {
            //状态不为200 直接返回错误
            return ResponseResult.errorResult(AppHttpCodeEnum.COMMENTS_IS_NON_COMPLIANCE_ERROR);
        }

        //3.2、判断返回的结果
        if (responseResult.getData() == CommentConstants.TWO || responseResult.getData() == CommentConstants.THREE
                || responseResult.getData() == CommentConstants.FOUR) {
            //返回错误结果
            return ResponseResult.errorResult(AppHttpCodeEnum.COMMENTS_IS_NON_COMPLIANCE_ERROR);
        }

        //4、保存评论回复数据
        ApCommentRepay apCommentRepay = new ApCommentRepay();
        //4.1、数据拷贝
        BeanUtils.copyProperties(commentRepaySaveDto, apCommentRepay);
        //4.2、设置作者id
        apCommentRepay.setAuthorId(userId);
        apCommentRepay.setLikes(0);                 //点赞数量 默认0
        apCommentRepay.setCreatedTime(new Date());  //创建时间
        apCommentRepay.setUpdatedTime(new Date());  //更新时间

        //4.3远程调用拿到作者名称
        ResponseResult response = iArticleClient.queryByUserId(userId);

        //4.4 获取里面数据
        Object data = response.getData();

        //4.5 将数据转换成ApAuthor对象
        ApAuthor apAuthor = JSON.parseObject(JSON.toJSONString(data), ApAuthor.class);

        log.info("获取到apAuthor为: {}", apAuthor);

        //4.6 设置作者名称
        apCommentRepay.setAuthorName(apAuthor.getName());

        //4.7修改MongoDB里的ap_comment表中的reply字段数量+1
        //先查询ap_comment表中的reply字段数量
        ApComment apComment = mongoTemplate.findById(commentRepaySaveDto.getCommentId(), ApComment.class);
        //判断是否为空
        if (apComment != null) {
            //修改ap_comment表中的reply字段数量 +1
            apComment.setReply(apComment.getReply() + 1);
        }

        //根据评论id更新mongoDB中的数据
        mongoTemplate.save(apComment);

        //4.3、保存数据到mongoDB apCommentRepay表中
        mongoTemplate.save(apCommentRepay);

        //4.4、返回结果
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 查询文章回复列表
     *
     * @param commentRepayDto
     * @return
     */
    @Override
    public ResponseResult commentRepayRoad(CommentRepayDto commentRepayDto) {
        //1、参数校验
        if (commentRepayDto == null || commentRepayDto.getCommentId() == null || commentRepayDto.getMinDate() == null) {
            //抛出参数异常
            throw new CustomException(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2、获取当前用户id
        Integer userId = ApThreadLocalUtil.get();

        //2.2判断当前用户是否登录
        if (userId == null) {
            //抛出需要登录异常
            throw new CustomException(AppHttpCodeEnum.NEED_LOGIN);
        }
        //3、查询评论列表，根据当前文章进行检索，按照创建时间倒序，分页查询（默认10条数据）
        List<ApCommentRepay> apCommentRepayList = mongoTemplate.find(Query
                        .query(Criteria
                                .where("commentId").is(commentRepayDto.getCommentId())
                                .and("createdTime").lt(commentRepayDto.getMinDate()))
                        .with(Sort.by(Sort.Direction.DESC, "createdTime"))
                        .limit(10),
                ApCommentRepay.class
        );
        //3.2 在结果返回中，如果当天登录人点赞了某条评论，需要高亮展示“**点赞按钮**”
        List<ApCommentRepayVo> apCommentRepayVos = apCommentRepayList.stream().map(apCommentRepay -> {
            ApCommentRepayVo apCommentRepayVo = new ApCommentRepayVo();
            //进行数据转移
            BeanUtils.copyProperties(apCommentRepay, apCommentRepayVo);
            //根据当前用户id和当前评论的id 查询对应的ApCommentLike数据
            List<ApCommentLike> apCommentLikes = mongoTemplate.find(Query.query(Criteria
                    .where("authorId").is(userId)           //用户id
                    .and("commentId").is(commentRepayDto.getCommentId())), ApCommentLike.class); //根据当前用户id和当前评论的id 查询对应的ApCommentLike数据
            //如果apCommentLikes集合不为空，说明当前评论被当前用户点赞
            if (apCommentLikes != null && apCommentLikes.size() > 0) {
                //设置点赞状态
                apCommentRepayVo.setOperation(0);
            }
            //返回
            return apCommentRepayVo;
        }).collect(Collectors.toList());
        return ResponseResult.okResult(apCommentRepayVos);
    }

    /**
     * 点赞 type = 2   -文章回复评论点赞
     */
    private ResponseResult getResult(LikesBehaviorDto dto, Integer userId) {
        //点赞判断状态
        if (dto.getOperation() == 1) {
            //取消点赞 operation=1
            String key = "like_" + userId + "_" + dto.getCommentRepayId() + "_" + dto.getType();
            //判断是否存在数据
            if (!redisTemplate.hasKey(key)) {
                redisTemplate.opsForValue().set(key, String.valueOf(dto.getOperation()));
            }
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        } else if (dto.getOperation() == 0) {
            //点赞 operation=1
            //记录用户行为存储到redis
            String key = "like_" + userId + "_" + dto.getCommentRepayId() + "_" + dto.getType();
            //判断是否存在数据
            if (!redisTemplate.hasKey(key)) {
                redisTemplate.opsForValue().set(key, String.valueOf(dto.getOperation()));
            }
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }
        //返回错误信息
        return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
    }


    /**
     * 评论 type = 2  -文章评论点赞
     *
     * @param dto
     * @param userId
     * @return
     */
    private ResponseResult getRepayResult(LikesBehaviorDto dto, Integer userId) {
        //点赞判断状态
        if (dto.getOperation() == 1) {
            //取消点赞 operation=1
            String key = "like_" + userId + "_" + dto.getCommentId() + "_" + dto.getType();
            //判断是否存在数据
            if (!redisTemplate.hasKey(key)) {
                redisTemplate.opsForValue().set(key, String.valueOf(dto.getOperation()));
            }
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        } else if (dto.getOperation() == 0) {
            //点赞 operation=1
            //记录用户行为存储到redis
            String key = "like_" + userId + "_" + dto.getCommentId() + "_" + dto.getType();
            //判断是否存在数据
            if (!redisTemplate.hasKey(key)) {
                redisTemplate.opsForValue().set(key, String.valueOf(dto.getOperation()));
            }
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }
        //返回错误信息
        return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
    }
}

